<template>
  <el-dialog
    v-model="props.visible"
    :footer="false"
    @close="emits('cancel')"
    width="800px"
  >
    <template #header="{ titleId, titleClass }">
      <div class="my-header flex">
        <h4
          :id="titleId"
          :class="titleClass"
        >
          <el-icon>
            <TakeawayBox />
          </el-icon>
          模板库
        </h4>
        <div class="search-box">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索模板"
            class="search-input"
          />
          <el-button
            link
            @click="handleSearch"
            class="search-button"
            :icon="Search"
          >
          </el-button>
        </div>
      </div>
    </template>
    <div
      class="dialog-content"
      v-show="show == 1"
      v-loading="dialogLoading"
    >
      <div class="empty-box">
        <div
          class="empty-Folder flex-center w-120 h-120"
          @click="AddEmptyFolder"
        >
          <div class="empty-Folder-icon w-p-60">
            <FolderAdd />
          </div>
          <p>空白文件夹</p>
        </div>
      </div>
      <div class="basicTemplate-box">
        <h4>基础模板</h4>
        <div class="flex flex-wrap">
          <div
            v-for="(item) in basicTemplateList"
            :key="item.id"
          >
            <div class="empty-Folder flex-center w-120 h-120">
              <div class="empty-Folder-icon w-p-60">
                <FolderAdd />
              </div>
              <p class="_p">{{ item.folderName }}</p>
            </div>
            <div class="flex-center mt-10">
              <el-button
                type="info"
                size="small"
                class="btn"
                @click="previewFolder(item)"
              >预览</el-button>
              <el-button
                type="info"
                size="small"
                class="btn"
                @click="useTemplate(item, '1')"
              >使用</el-button>
            </div>
          </div>
        </div>
      </div>
      <div class="myTemplate-box">
        <h4>我的模板</h4>
        <div v-show="!myTemplateList.length">-无数据-</div>
        <div
          v-show="myTemplateList.length"
          class="flex flex-wrap"
        >
          <div
            v-for="item in myTemplateList"
            :key="item.id"
          >
            <div class="empty-Folder flex-center w-120 h-120">
              <div class="empty-Folder-icon w-p-60">
                <FolderAdd />
              </div>
              <p class="_p">{{ item.folderName }}</p>
            </div>
            <div class="flex-center mt-10">
              <el-button
                type="info"
                size="small"
                class="btn"
                @click="previewFolder(item)"
              >预览</el-button>
              <el-button
                type="info"
                size="small"
                class="btn"
                @click="useTemplate(item, '2')"
              >使用</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div
      class="dialog-content"
      v-show="show == 2"
      v-loading="dialogLoading"
    >
      <div class="folderInfo">
        <div class="folderNameBox">
          <div>{{ selectedTemplate?.folderName }}</div>
          <div>
            <el-button
              type="default"
              :icon="ArrowLeftBold"
              size="small"
              link
              @click="returnLastPage"
            >返回</el-button>
          </div>
        </div>
        <div class="remarkBox">
          {{
            selectedTemplate?.remark == ""
              ? "暂无备注"
              : selectedTemplate?.remark
          }}
        </div>
      </div>
      <div class="fileList flex flex-wrap">
        <div
          v-for="(item) in templateDocument"
          :key="item.id"
          class="file"
        >
          <div class="empty-Folder flex-center w-120 h-120">
            <div class="empty-Folder-icon w-p-60">
              <img
                class="icon_box"
                :src="menu_document"
              />
              <!-- <FolderAdd /> -->
            </div>
            <p class="_p">{{ item.fileName }}</p>
          </div>
          <div class="flex-center mt-10">
            <el-button
              type="info"
              size="small"
              class="btn"
              @click="previewFile(item)"
            >
              预览
            </el-button>
            <!-- <el-button
              type="info"
              size="small"
              class="btn"
              @click="useFolder(item)"
            >
              使用
            </el-button> -->
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
  <!-- <el-dialog
    v-model="fullScreendialogVisible"
    title="文档预览"
    fullscreen
    destroy-on-close
    @close="fullScreendialogVisible = false"
  >
    <div class="container">
      <jodit
        :htmlStructure="htmlStructure"
        :isReadonly="true"
        :isToolbar="false"
      />
    </div>
    <template #footer>
      <el-button @click="fullScreendialogVisible = false">关闭</el-button>
    </template>
  </el-dialog> -->
  <FSPreFile :htmlStructure="htmlStructure" :visible="fullScreendialogVisible" @close="fullScreendialogVisible = false" />
</template>
<script setup>
import { Search, CirclePlus, ArrowLeftBold } from "@element-plus/icons-vue";
import { addFolder } from "@/api/folder";
import { ElMessage } from "element-plus";
import jodit from "@/views/fileEdit/jodit.vue";
import FSPreFile from "./fullScreenPreviewFile.vue"
import menu_document from "@/assets/images/menu_document.png";
import {
  getMyTemplateList,
  getTemplateFolder,
  getTemplateInfo,
  addPersonalTemplateFolder,
  updatePersonalTemplateFolder,
  topPersonalTemplateFolder,
  deleteTemplate,
} from "@/api/template.js";
import { getTemplateDocument } from "@/api/document.js";
import { getAllData } from "@/utils/getData.js";
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  row: {
    type: Object,
    default: () => { },
  },
});
const emits = defineEmits(["close", "cancel"]); //两者区别在于一个需要重新获取列表数据一个不需要
const show = ref(1);
const dialogLoading = ref(false);
const searchKeyword = ref("");
const handleSearch = () => {
  console.log("handleSearch");
};
const AddEmptyFolder = () => {
  console.log("AddEmptyFolder");
  addFolder({
    creationMode: "0", // 0,1,2
    // "templateFolderId": "1" // 公共模板id
    // "templateFolderId": "1" // 我的模板id
  }).then((res) => {
    console.log("res", res);
    if (res.code == 200) {
      ElMessage({
        message: "新建成功!",
        type: "success",
      });
      emits("close");
    }
  });
};
const basicTemplateList = ref([]);
const myTemplateList = ref([]);
const selectedTemplate = ref(null);
const templateDocument = ref([]);
const previewFolder = (item) => {
  console.log("previewFolder", item);
  selectedTemplate.value = item;
  // 获取文件夹详情和文件列表
  // dialogLoading.value = true;
  // getTemplateInfo(item.id).then((res) => {
  //   console.log("getTemplateInfo", res);
  // });//得到的内容就是item//没用
  templateDocument.value = item.edPersonageTemplateDocumentVoList;
  show.value = 2;
};
const returnLastPage = () => {
  show.value = 1;
};
const useTemplate = (item, mode) => {
  console.log(item, mode);

  // 新建文件夹
  addFolder({
    creationMode: mode, // 0,1,2
    templateFolderId: item.id, // 模板id
  }).then((res) => {
    console.log("res", res);
    if (res.code == 200) {
      ElMessage({
        message: "新建成功!",
        type: "success",
      });
      emits("close");
    }
  });
};
const fullScreendialogVisible = ref(false);
const htmlStructure = ref("");
const previewFile = (item) => {
  console.log(item);
  htmlStructure.value = item.htmlStructure || '';
  // 打开全屏预览
  fullScreendialogVisible.value = true;
};
// document.getElementById("mask").addEventListener("scroll", function (e) {
//   e.preventDefault();
//   document.getElementById("mask").style.display = "none";
// });
onMounted(() => {
  // 获取基础模板
  getTemplateFolder({ pageSize: 999, pageNum: 1 }).then((res) => {
    console.log(res, "获取基础模板");
    console.log(res);
    if (res.code == 200) {
      basicTemplateList.value = res.rows;
    }
  });
  // 获取我的模板
  getMyTemplateList().then((res) => {
    console.log(res);
    if (res.code == 200) {
      myTemplateList.value = res.data;
    }
  });
});
</script>
<style lang="scss" scoped>
.el-dialog__title {
  display: flex;
  align-items: center;
  gap: 10px;
}
.my-header {
  height: 34px;
  justify-content: space-between;
  .search-box {
    display: flex;
    align-items: center;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    margin-right: 15px;
    .search-input {
      flex: 1;
      border-right: 1px solid #dcdfe6;
      :deep(.el-input__wrapper) {
        box-shadow: none;
      }
      .el-input__icon {
        cursor: pointer;
        font-size: 18px;
        &:hover {
          color: #409eff; /* Element Plus 主色调 */
        }
      }
    }
    .search-button {
      height: 100%;
      padding: 0 8px;
    }
  }
}
.dialog-content {
  height: 60vh;
  overflow-y: auto;
  .folderInfo {
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    // height: 50px;
    padding: 10px;
    .folderNameBox {
      display: flex;
      justify-content: space-between;
    }
    .remarkBox {
      padding: 10px 10px 0;
      color: #999;
    }
  }
  .fileList {
    // box-sizing: border-box;
    padding: 20px;
    gap: 20px;
    .file {
      display: flex;
      flex-direction: column;
    }
  }
}
// .empty-box {
.empty-Folder {
  border: 1px solid #ccc;
  border-radius: 5px;
  flex-direction: column;
  padding: 10px;
  ._p {
    margin-top: 5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
    text-align: center;
  }
}
// }
.basicTemplate-box,
.myTemplate-box {
  margin: 30px 0;
  h4 {
    line-height: 50px;
  }
  .flex.flex-wrap {
    gap: 20px;
  }
}
.icon_box {
  width: 100%;
}

</style>
